package com.example.medicalapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.medicalapp.R;
import com.example.medicalapp.model.Prescription;
import com.example.medicalapp.model.User;
import com.example.medicalapp.helper.DatabaseHelper;

import java.util.List;

public class PrescriptionAdapter extends RecyclerView.Adapter<PrescriptionAdapter.PrescriptionViewHolder> {

    private List<Prescription> prescriptionList;
    private OnItemClickListener listener;
    private boolean isPatientView; // 区分是患者视图还是医生视图
    private DatabaseHelper dbHelper;

    // 点击事件接口
    public interface OnItemClickListener {
        void onItemClick(Prescription prescription);
    }

    public PrescriptionAdapter(List<Prescription> prescriptionList, boolean isPatientView,
                               OnItemClickListener listener, DatabaseHelper dbHelper) {
        this.prescriptionList = prescriptionList;
        this.isPatientView = isPatientView;
        this.listener = listener;
        this.dbHelper = dbHelper;
    }

    @NonNull
    @Override
    public PrescriptionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_prescription, parent, false);
        return new PrescriptionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PrescriptionViewHolder holder, int position) {
        Prescription prescription = prescriptionList.get(position);

        // 显示诊断结果作为标题
        holder.tvTitle.setText(prescription.getFinalDiagnosis());

        // 根据视图类型显示不同的信息（患者视图显示医生名，医生视图显示患者名）
        if (isPatientView) {
            // 患者视图：显示医生信息
            User doctor = dbHelper.getUserById(prescription.getDoctorId());
            if (doctor != null) {
                holder.tvDoctorOrPatient.setText("医生：" + doctor.getUsername());
            } else {
                holder.tvDoctorOrPatient.setText("医生：未知");
            }
        } else {
            // 医生视图：显示患者信息
            User patient = dbHelper.getUserById(prescription.getPatientId());
            if (patient != null) {
                holder.tvDoctorOrPatient.setText("患者：" + patient.getUsername());
            } else {
                holder.tvDoctorOrPatient.setText("患者：未知");
            }
        }

        // 显示日期和处方摘要
        holder.tvDate.setText("诊断日期：" + prescription.getCreatedDate().split(" ")[0]);
        holder.tvSummary.setText(prescription.getFinalPrescription());

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(prescription);
            }
        });
    }

    @Override
    public int getItemCount() {
        return prescriptionList.size();
    }

    // ViewHolder类
    public static class PrescriptionViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvDoctorOrPatient;
        TextView tvDate;
        TextView tvSummary;

        public PrescriptionViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvDoctorOrPatient = itemView.findViewById(R.id.tv_doctor_or_patient);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvSummary = itemView.findViewById(R.id.tv_summary);
        }
    }
}